﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace Client.Download
{
    public class DownloadFile
    {
        
        public DownloadFile(string ip,int port,string path)
        {

            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            client.Connect(IPAddress.Parse(ip), port);
            client.ReceiveBufferSize = 512000;
            Console.WriteLine("Connected");
            ThreadPool.QueueUserWorkItem(new WaitCallback(Recive), client);
            this.path = path;
        }
        public void SendRequest(int id)
        {
            this.id = id;
            List<byte> buffer = new List<byte>();
            buffer.Add(0);
            buffer.AddRange(BitConverter.GetBytes(id));
            client.Send(buffer.ToArray());
            wait.WaitOne();
        }
        void Recive(object state)
        {
            Socket sock = state as Socket;
            byte[] buffer = new byte[1];
            while (true)
            {
                if (sock.Available != 0 && Process == false)
                {
                    //ReadHeader
                    buffer = new byte[1];
                    sock.Receive(buffer);
                    switch ((MSG)buffer[0])
                    {
                        case MSG.FILE_SIZE:
                            buffer = new byte[8];
                            sock.Receive(buffer);
                            Size = BitConverter.ToInt32(buffer, 0);
                            reader = new StreamWriter(path+"\\_"+id.ToString()+".bin");
                            Console.WriteLine("Size :" + Size);
                            break;
                        case MSG.BEGIN_SEND:
                            Process = true;
                            Console.WriteLine("Begin Recive");
                            break;
                    }
                }
                else if (sock.Available != 0 && Process == true)
                {
                    buffer = new byte[8000];
                    int leng = sock.Receive(buffer);
                    Size -= leng;
                    if (Size == 0)
                    {
                        reader.BaseStream.Write(buffer, 0, leng);
                        reader.Close();
                        Process = false;
                        Console.WriteLine("Recive Complete");
                        wait.Set();
                    }
                    else
                        reader.BaseStream.Write(buffer, 0, leng);
                }
                if (!Process)
                    Thread.Sleep(200);
            }
        }
        int id;
        bool Process = false;
        StreamWriter reader;
        Int64 Size;
        string path;
        Socket client;
        AutoResetEvent wait = new AutoResetEvent(false);
    }
}
